package com.ntschy.web.controller.bt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.bt.entity.dataobject.BtHelp.BtHelp;
import com.ntschy.bt.entity.dataobject.BtHelp.SysDept;
import com.ntschy.bt.entity.dto.BtHelpDTO.AssignDTO.BtHelpAssignAllocationDTO;
import com.ntschy.bt.entity.dto.BtHelpDTO.AssignDTO.BtHelpAssignPageDTO;
import com.ntschy.bt.entity.dto.BtHelpDTO.ReviewDTO.BtHelpAuditDTO;
import com.ntschy.bt.entity.dto.BtHelpDTO.ReviewDTO.BtHelpReviewPageDTO;
import com.ntschy.bt.entity.enums.BtHelpBackEnum;
import com.ntschy.bt.entity.enums.BtHelpExamineResultEnum;
import com.ntschy.bt.entity.enums.BtHelpStatusEnum;
import com.ntschy.bt.entity.mapstruct.BtHelpMapstruct;
import com.ntschy.bt.entity.mapstruct.BtHelpReviewMapstruct;
import com.ntschy.bt.entity.vo.BtHelp.Assign.BtHelpAssignDetailsVO;
import com.ntschy.bt.entity.vo.BtHelp.Assign.BtHelpAssignPageVo;
import com.ntschy.bt.entity.vo.BtHelp.Review.BtHelpReviewPageVO;
import com.ntschy.bt.service.IBtHelp.IBtHelpAssignService;
import com.ntschy.bt.service.IBtHelp.IBtHelpReviewService;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *
 * </p>
 *
 * @author
 * @since
 */
@Validated
@RestController
@RequestMapping("/bt/help")
public class BtHelpReviewController extends BaseController {

    @Resource
    private IBtHelpReviewService iBtHelpReviewService;

    @Resource
    private IBtHelpAssignService iBtHelpAssignService;
    // * ================================================基础增删改查BEGIN============================================= *

    /**
     * 一键求助分配部门审核分页列表
     *
     * @param pageDTO
     * @return
     */
    @ApiOperation(value = "查询一键求助列表", notes = "")
    @ApiImplicitParam(name = "pageSupplyShop", value = "分配部门审核分页查询", required = true, dataType = "BtHelpReviewPageDTO")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @PostMapping("/pageWaitAuditHelp")
    public AjaxPageResult<BtHelpReviewPageVO> pageSupplyShop(@RequestBody BtHelpReviewPageDTO pageDTO) {
        Page<BtHelp> page = PageHelper.startPage(pageDTO.getCurrPage(), pageDTO.getPageSize());
        BtHelp bthelp = BtHelpReviewMapstruct.BT_HELP_Review_MAP_STRUCT.pagetableDto(pageDTO);
        List<BtHelp> btHelps = iBtHelpReviewService.pageHelpReview(bthelp);
        List<BtHelpReviewPageVO> btHelpAssignPageVos = BtHelpReviewMapstruct.BT_HELP_Review_MAP_STRUCT.pagePustrespone(btHelps);
        return buildPageResult(btHelpAssignPageVos,page);

    }


    /**
     * 一键求助分配部门审核详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一键求助分配部门详情", notes = "")
    @ApiImplicitParam(name = "fetchWaitAuditHelp", value = "分配部门详情", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @GetMapping("/fetchWaitAuditHelp")
    public AjaxResult<Map> fetchWaitAuditHelp(@NotBlank(message = "ID不能为空") String id){
        BtHelp btHelp = iBtHelpAssignService.detailsHelpAssing(id);
        BtHelpAssignDetailsVO btHelpAssignDetailsVO = BtHelpMapstruct.BT_HELP_ASSIGN_MAP_STRUCT.mainchangeBtHelpAssignDetailsVO(btHelp);
        List<SysDept> selectdept = iBtHelpAssignService.selectdept();
        Map datamap = new HashMap<>();
        datamap.put("btHelpAssignDetailsVO", btHelpAssignDetailsVO);
        datamap.put("selectdept", selectdept);
        return AjaxResult.success("成功",datamap);

    }


    /**
     * 分配部门审核
     *
     * @param btHelpAuditDTO
     * @return
     */
    @ApiOperation(value = "分配部门审核", notes = "")
    @ApiImplicitParam(name = "updateAllotHelp", value = "分配部门审核", required = true, dataType = "btHelpAuditDTO")
    //@PreAuthorize("@ss.hasPermi('bt:supplyshop:page')")
    @PostMapping("/updateAuditHelp")
    public AjaxResult<String> updateAuditHelp(@RequestBody BtHelpAuditDTO btHelpAuditDTO){
        BtHelpAuditDTO helpauditdto = btHelpAuditDTO;
        BtHelp btHelp = BtHelpReviewMapstruct.BT_HELP_Review_MAP_STRUCT.minBtHelpAuditDTO(helpauditdto);
        BtHelp realitybtHelp = iBtHelpAssignService.detailsHelpAssing(btHelp.getId());
        int  Theresults = 0;
        if(realitybtHelp.getStatus() == BtHelpStatusEnum.PENDING_REVIEW){
            if(btHelp.getExamineResult() == BtHelpExamineResultEnum.YES &&  btHelp.getReplayOpinion() == null){
                btHelp.setExamineTime(iBtHelpReviewService.newData());
                btHelp.setStatus(BtHelpStatusEnum.TO_REPLY);
                btHelp.setBack(BtHelpBackEnum.NO);
                Theresults = iBtHelpReviewService.updateAudit(btHelp);
            }

            if(btHelp.getExamineResult() == BtHelpExamineResultEnum.NO){
                btHelp.setExamineTime(iBtHelpReviewService.newData());
                btHelp.setStatus(BtHelpStatusEnum.TO_BE_ASSIGNED);
                btHelp.setBack(BtHelpBackEnum.YES);
                Theresults = iBtHelpReviewService.updateAudit(btHelp);
            }
        }
        return  toAjax(Theresults,"审核成功","审核失败");
    }





    // * ================================================基础增删改查BEGIN============================================= *


}
